<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <!--
                        <el-date-picker v-model="queryForm.timeVal" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small" type="daterange" placement="bottom-end" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" @change="onchangeTime" />
             -->



            <el-form-item>

              <el-select v-model="queryForm.appid" placeholder="请选择app" class="width100" filterable clearable>
                <el-option
                  v-for="item in applist"
                  :key="item.appid"
                  :value="item.appid"
                  :label="item.appname">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>

              <el-select v-model="queryForm.adplatform" placeholder="请选择广告平台" class="width100" filterable clearable>
                <el-option
                  v-for="item in selOptions.adplatform"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.taskname" placeholder="请输入任务名称" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-date-picker v-model="queryForm.timeVal" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small"
                              type="daterange" placement="bottom-end" start-placeholder="开始日期"
                              end-placeholder="结束日期" style="width: 250px;" @change="onchangeTime"/>
            </el-form-item>

            <el-form-item>
              <el-button @click="search()">查询</el-button>
              <el-button v-if="checkPermi(['task:index:save'])" type="primary" @click="addOrUpdateHandle()">新增
              </el-button>
              <el-button v-if="checkPermi(['task:index:save'])" type="primary" @click="copyHandle()"
                         :disabled="dataListSelections.length <= 0">复制
              </el-button>
              <el-button v-if="checkPermi(['task:index:delete'])" type="danger" @click="deleteHandle()"
                         :disabled="dataListSelections.length <= 0">批量
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
                style="width: 100%;" size="mini" :highlight-current-row="true"
                :header-cell-style=" {fontWeight:'bold'}">
        <el-table-column type="selection" header-align="center" align="center" width="50"/>
        <el-table-column prop="id" label="主键" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="icon" label="icon" >
          <template slot-scope="scope"  >
            <el-image  fit="scaleDown"
                       :src="scope.row.icon" :preview-src-list="[scope.row.icon]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="taskname" label="任务名称" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="taskinfo" label="任务描述" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="coin" label="金币" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="limitcount" label="任务完成数量" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="adtype" label="广告类型 " :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-tag type="success" size="mini" v-if="scope.row.adtype==0">开屏广告</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==1">banner广告</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==2">激励视频</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==3">插屏广告</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.adtype==4">语音红包</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="adplatform" label="广告平台 " :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="viptype" label="vip类型" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.viptype===0">普通任务</el-tag>
            <el-tag size="mini" type="warn" v-if="scope.row.viptype===1">VPI1任务</el-tag>
            <el-tag size="mini" type="warn" v-if="scope.row.viptype===2">VPI2任务</el-tag>
            <el-tag size="mini" type="error" v-if="scope.row.viptype===3">VPI3任务</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="appname" label="app" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="ordersum" label="排序" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status==0"
              :active-value="true"
              :inactive-value="false"
              active-text="正常"
              inactive-text="下架"
              @change="change(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createtime" label="创建时间"  width="150">
        </el-table-column>

        <!--
<template slot-scope="scope" > <el-tag type="success" v-if="scope.row.type===0">免费</el-tag><el-tag type="warn" v-else>付费</el-tag></template>
<template slot-scope="scope" ><el-image style="width: 80px !important;height: 100% !important"  fit="scaleDown" :src="scope.row.icon" :preview-src-list="[scope.row.icon]"/></template>
-->
        <el-table-column v-if="checkPermi(['task:index:update'])&&checkPermi(['task:index:delete'])"  fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" v-if="checkPermi(['task:index:update'])" size="small"
                       @click="addOrUpdateHandle(scope.row.id)">修改
            </el-button>
            <el-button type="text" v-if="checkPermi(['task:index:delete'])" size="small"
                       @click="deleteHandle(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="tableForm.pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tableForm.pageSize"
        :total="tableForm.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './task-add-and-update'
import api from '@/api/api'
import utils from '@/api/utils'
import { checkPermi } from '@/utils/permission'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      selOptions: {},
      dataList: [],
      applist:[],
      queryForm: {
        id: '',
        appid:'',
        taskname: '',
        taskinfo: '',
        coin: '',
        limitcount: '',
        adtype: '',
        adplatform: '',
        viptype: '',
        ordersum: '',
        status: '',
        createtime: '',
        updatetime: '',
        createtime_begin: '',
        createtime_end: '',
        timeVal: []
      },
      tableForm: {
        pageIndex: 1,
        pageSize: 10,
        total: 0
      },
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted: function() {
    this.selOptions = this.$constants.selOptions
    utils.getAppList().then(res => {this.applist=res.datalist});
    this.search()
  },
  methods: {
    checkPermi,
    // 每页数
    sizeChangeHandle(val) {
      this.tableForm.pageSize = val
      this.tableForm.pageIndex = 1
      this.search()
    },
    // 当前页
    currentChangeHandle(val) {
      this.tableForm.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    change(row) {
      let postData = { id: row.id }
      postData.status = row.status == 1 ? 0 : 1
      api.post('rest/vabc/task/updateById', postData).then(res => {
        this.$message.success('操作成功')
        row.status = postData.status
        // this.getDataList()
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let postData = { ids: ids.join(',') }
        api.post('rest/vabc/task/delete', postData).then(res => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      })
    },
    search() {
      this.tableForm.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.dataListLoading = true
      let postData = {
        'order': 'desc',
        'column': 'id'
      }
      Object.assign(postData, this.queryForm, this.tableForm)
      //postData.name=api.getLikeValue( postData.name);
      postData.taskname = api.getLikeValue(postData.taskname)
      postData.taskinfo = api.getLikeValue(postData.taskinfo)
      postData.coin = api.getLikeValue(postData.coin)
      postData.limitcount = api.getLikeValue(postData.limitcount)
      postData.adtype = api.getLikeValue(postData.adtype)
      postData.adplatform = api.getLikeValue(postData.adplatform)
      postData.viptype = api.getLikeValue(postData.viptype)
      postData.ordersum = api.getLikeValue(postData.ordersum)
      postData.status = api.getLikeValue(postData.status)
      postData.createtime = api.getLikeValue(postData.createtime)
      postData.updatetime = api.getLikeValue(postData.updatetime)
      api.post('rest/vabc/task/list', postData).then(res => {
        this.dataList = res.datalist
        this.tableForm.total = res.total
      }).finally(() => {
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    copyHandle() {
      this.$prompt("请输入任务复制appid", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'text',
        inputPattern: /^.+$/,
        inputErrorMessage: '请输入要复制的appid'
      }).then(({ value }) => {
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`您确定将[id=${ids.join(',')}]任务复制到该App吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let postData = { ids: ids.join(',') }
          postData.appid = value
          api.post('rest/vabc/task/copy', postData).then(res => {
            this.$message.success('复制成功')
            this.queryForm.appid=value
            this.getDataList()
          })
        })
      }).catch(() => {
      })
    },
    onchangeTime(e) {
      if (e) {
        this.queryForm.createtime_begin = e[0]
        this.queryForm.createtime_end = e[1]
      } else {
        this.queryForm.createtime_begin = ''
        this.queryForm.createtime_end = ''
      }

    }
  }
}
</script>
